/// <summary>
/// Copyright 2010 Ivo Limmen (ivo@limmen.org)
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// 
/// http://www.apache.org/licenses/LICENSE-2.0
/// 
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// </summary>

using System;
using System.Diagnostics;

using ServiceController.Api;
using Services.Logging.Api;
using Util.Logging;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using log4net;
using log4net.Layout;
using log4net.Core;

namespace Services.Logging
{
	public class LogService : BaseService, ILogService
	{
		public LogService() : base("LogService", "Provides the ability to log messages")
		{
		}

        public override void Initialize()
        {
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

            ConsoleAppender consoleAppender = new ConsoleAppender();
            consoleAppender.Layout = new PatternLayout("%date [%thread] %-5level %logger - %message%newline");

            hierarchy.Root.Additivity = true;
            hierarchy.Root.AddAppender(consoleAppender);
            hierarchy.Root.Level = Level.All;

            Logger omniServiceLogger = (Logger)hierarchy.GetLogger("ServiceController");

            RollingFileAppender fileAppender = new RollingFileAppender();
            fileAppender.File = "service-controller.log";
            fileAppender.Layout = new PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            fileAppender.ImmediateFlush = true;
            fileAppender.RollingStyle = RollingFileAppender.RollingMode.Size;
            fileAppender.MaximumFileSize = "10MB";
            fileAppender.AppendToFile = true;
            fileAppender.MaxSizeRollBackups = 10;
            fileAppender.Threshold = Level.Error;
            fileAppender.ActivateOptions();

            omniServiceLogger.Additivity = true;
            omniServiceLogger.AddAppender(fileAppender);

            hierarchy.Configured = true;
        }


		#region ILogService implementation

		public Services.Logging.Api.ILogger GetLogger(string source)
		{
			return new LogWrapper(LogManager.GetLogger(source));
		}
		
		#endregion
	}
}
